package com.wangxiaonan.wxwork.quartz;

import com.wangxiaonan.wxwork.config.QuartzConfig;
import com.wangxiaonan.wxwork.exeption.ServiceException;
import com.wangxiaonan.wxwork.service.TaskService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

/**
 * 文本类型日报任务
 *
 * @Author: wangxiaonan
 * @Date: 2018/8/29
 **/
public class DailyTextMessageJob implements Job {
    private static final Logger logger = LoggerFactory.getLogger(DailyTextMessageJob.class);

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            ApplicationContext applicationContext = (ApplicationContext) context.
                    getScheduler()
                    .getContext()
                    .get(QuartzConfig.APPLICATION_CONTEXT_KEY);
            TaskService taskService = applicationContext.getBean(TaskService.class);
            JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
            taskService.dailyTask(jobDataMap.getWrappedMap());
        } catch (ServiceException e) {
            logger.error("[DailyTextMessageJob] execute job fail >>>>> {}", e.getMessage());
        } catch (BeansException e) {
            logger.error("[DailyTextMessageJob] execute init bean fail >>>>> {}", e.getMessage());
        } catch (NullPointerException | SchedulerException e) {
            logger.error("[DailyTextMessageJob] execute fail >>>>> {}", e.getMessage());
        }
    }
}
